<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input
        v-model="formData.name"
        :maxlength="50"
        placeholder="请输入数据标题，不超过50个字符"
      />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        :maxlength="200"
        @keyup.enter.native="specialFormValidate('desc')"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>

    <el-form-item label="位置">
      <div class="row-item">
        <div class="title">经度</div>
        <el-input-number
          v-model="formData.attr.position[0]"
          placeholder=""
          :controls="false"
          :min="-180"
          :max="180"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">纬度</div>
        <el-input-number
          v-model="formData.attr.position[1]"
          placeholder=""
          :controls="false"
          :min="-90"
          :max="90"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">°</div>
      </div>
      <div class="row-item">
        <div class="title">高度</div>
        <el-input-number
          v-model="formData.attr.position[2]"
          placeholder=""
          :controls="false"
          :precision="2"
          @keyup.enter.native="specialFormValidate('position')"
        />
        <div class="unit">米</div>
      </div>
    </el-form-item>

    <el-form-item label="参数">
      <div class="row-item param">
        <div class="title">半长轴长度</div>
        <el-input
          v-model="formData.attr.semiMajorAxis"
          placeholder=""
          @keyup.enter.native="specialFormValidate('semiMajorAxis')"
        />
        <div class="unit">米</div>
      </div>
      <div class="row-item param">
        <div class="title">半短轴长度</div>
        <el-input
          v-model="formData.attr.semiMinorAxis"
          placeholder=""
          @keyup.enter.native="specialFormValidate('semiMinorAxis')"
        />
        <div class="unit">米</div>
      </div>
      <div class="row-item param">
        <div class="title">速度</div>
        <el-input
          v-model="formData.attr.speed"
          placeholder=""
          @keyup.enter.native="specialFormValidate('speed')"
        />
        <div class="unit"></div>
      </div>
      <div class="row-item param">
        <div class="title">圆形个数</div>
        <el-input
          v-model="formData.attr.circleCount"
          placeholder=""
          @keyup.enter.native="specialFormValidate('circleCount')"
        />
        <div class="unit">个</div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';
import { useSceneStore } from '@/store/scene';

defineOptions({
  name: 'FakeParticleSystem',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const sceneStore = useSceneStore();
const formData = ref<any>(null);
const emits = defineEmits(['formValidate']);

function formValidate(fieldName: string, valid: boolean) {
  // emits('formValidate', fieldName, valid);
  updateAttr(fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['position'].includes(fieldName)) {
    for (const list of formData.value.attr[fieldName]) {
      if (list === '' || isNaN(list)) {
        valid = false;
      }
    }
  } else if (['semiMajorAxis', 'semiMinorAxis', 'speed', 'circleCount'].includes(fieldName)) {
    if (formData.value.attr[fieldName] === '' || isNaN(formData.value.attr[fieldName])) {
      valid = false;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  }
  if (
    ['semiMajorAxis', 'semiMinorAxis'].includes(fieldName) &&
    formData.value.attr.semiMajorAxis < formData.value.attr.semiMinorAxis
  ) {
    valid = false;
    messageUtil.warning('半长轴必须大于等于半短轴');
  }
  // emits('formValidate', fieldName, valid);
  updateAttr(fieldName, valid);
}

function updateAttr(fieldName: string, valid: boolean) {
  const { cimInstance } = window;
  sceneStore.editError = !valid;
  if (sceneStore.editError || !sceneStore.selLayer) return;
  const config = {
    ...sceneStore.selLayer.attr.config,
    longitude: Number(sceneStore.selLayer.attr.position[0]),
    latitude: Number(sceneStore.selLayer.attr.position[1]),
    height: Number(sceneStore.selLayer.attr.position[2]),
    semiMajorAxis: Number(sceneStore.selLayer.attr.semiMajorAxis),
    semiMinorAxis: Number(sceneStore.selLayer.attr.semiMinorAxis),
    speed: Number(sceneStore.selLayer.attr.speed),
    circleCount: Number(sceneStore.selLayer.attr.circleCount),
  };
  cimInstance.api.updateLayerGraphics(sceneStore.selLayer.layerName, {
    pose: {
      position: sceneStore.selLayer.attr.position.map((v: any) => Number(v)),
      translation: [0, 0, 0],
      rotation: sceneStore.selLayer.attr.rotation.map((v: any) => Number(v)),
      scale: Number(sceneStore.selLayer.attr.scale),
    },
    color: sceneStore.selLayer.attr.color || '',
  });
  const position = {
    x: Number(sceneStore.selLayer.attr.position[0]),
    y: Number(sceneStore.selLayer.attr.position[1]),
    z: Number(sceneStore.selLayer.attr.position[2]),
  };
  const rotation = {
    x: Number(sceneStore.selLayer.attr.rotation[0]),
    y: Number(sceneStore.selLayer.attr.rotation[1]),
    z: Number(sceneStore.selLayer.attr.rotation[2]),
  };
  cimInstance.api.updateEffectModel({
    position,
    rotation,
    scale: Number(sceneStore.selLayer.attr.scale),
  });
  cimInstance.api.refreshOrbitController();
  cimInstance.api.replaceParticleSystem(sceneStore.selLayer.json.components[0].name, config);
  if (sceneStore.effectDotState) {
    sceneStore.effectTemporaryAttr = {
      ...config,
    };
  }
}

watchEffect(() => {
  formData.value = props.form;
});
</script>

<style lang="less" scoped>
.el-form {
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input,
      .el-input-number {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      &.param {
        .title {
          flex: 0 0 80px;
        }
      }
      &:last-child {
        margin-bottom: 0px;
      }
    }
  }
}
</style>
